package com.atguigu.educenter.service.impl;


import cn.hutool.crypto.SecureUtil;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author qing
 * @since 2021-08-26
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 用户登录
     * @param member
     * @return
     */
    @Override
    public String login(UcenterMember member) {
        //获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null) {//没有这个手机号
            throw new GuliException(20001,"登录失败");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        String password1 = mobileMember.getPassword();
        String md5password = SecureUtil.md5(password);
        if(!md5password.equals(password1)) {
            throw new GuliException(20001,"登录失败");
        }

        //判断用户是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new GuliException(20001,"登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {

        //获取注册数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //非空的判断
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)
            ||StringUtils.isEmpty(code) ||StringUtils.isEmpty(nickname)
        ){
            throw new GuliException(20001,"空参,注册失败");
        }
        //验证码判断,获取redis验证码。如果redis密码不存在就重新发送验证码
        Object o = redisTemplate.opsForValue().get(mobile);
        String redisCode = String.valueOf(o);
        if(!code.equals(redisCode)) {
            throw new GuliException(20001,"验证码输入错误");
        }
        //如果手机号已存在就不能添加
        QueryWrapper<UcenterMember> ucenterMemberQW = new QueryWrapper<>();
        ucenterMemberQW.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(ucenterMemberQW);
        if (count>=1){
            throw new GuliException(20001,"手机号已存在");
        }

        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(SecureUtil.md5(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://online-teach-file.oss-cn-beijing.aliyuncs.com/teacher/2019/10/30/de47ee9b-7fec-43c5-8173-13c5f7f689b2.png");

        //添加数据
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> ucenterMemberQW = new QueryWrapper<>();
        ucenterMemberQW.eq("openid",openid)
                .eq("is_deleted",0);
        UcenterMember ucenterMember = baseMapper.selectOne(ucenterMemberQW);
        return ucenterMember;
    }

    @Override
    public Integer countRegister(String day) {

        return  baseMapper.countRegister(day);
    }
}
